package ru.housewife.controllers.user;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ru.common.hibernate.HibernateActionExecutor;
import ru.housewife.controllers.AuthorizedController;
import ru.housewife.form.UserForm;
import ru.housewife.form.UsersTableBriefBlockForm;
import ru.housewife.form.UsersTableForm;

import javax.servlet.http.HttpServletRequest;

import java.util.LinkedList;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * controller for authorized users
 *
 * @author Alexander Troshanin
 */
@Controller
public class AuthorizedUserController extends AuthorizedController {

    private final HibernateActionExecutor hiber;

    public AuthorizedUserController(@Qualifier("houseWifesHibernate") HibernateActionExecutor hiber) {
        this.hiber = hiber;
    }

    @RequestMapping(value = "/main.html", method = GET)
    public ModelAndView getMainView(HttpServletRequest request) {
        // TODO: here we might have some megasupperpupper method wich will generate breaf info about user to show to him
        // not it is just a sub:
        ModelAndView ret = new ModelAndView("housewifes/users/userHomePage");
        UserForm user = getUserForm(request);
        List<UsersTableBriefBlockForm> usersTablesBriefs = new LinkedList<UsersTableBriefBlockForm>();
        user.setUsersTableBriefBlocks(usersTablesBriefs);
        for (UsersTableForm userTable : user.getUsersTables()) {
            if (userTable.isActive()) {
                usersTablesBriefs.add(userTable.getUsersTableBriefBlockForm());
            }
        }
        ret.addObject("user", user);
        ret.addObject("usersTablesBriefs", usersTablesBriefs);
        return ret;
    }

}
